/* 
 * shamefully copy from AnFetion.
 * more see: http://code.google.com/p/anfetion/
 */
package person.bangbang.im.Androidgin.Fetion;

import android.content.Context;
import android.content.SharedPreferences;

public final class Settings {
    public static final boolean DEBUG = true;
    public static final String TAG = "AnFetion";
    public static final String PREFS_USER_RECORD = "AnFetionUser";
    
    public static final String FAKE_VERSION = "3.5.0870";

    public static final String ACTION_RECEIVE_MESSAGE = "com.cngphone.AnFetion.FetionListFriends.RECEIVE_MESSAGE";
    public static final String ACTION_FRIEND_CHANGE = "com.cngphone.AnFetion.FetionListFriends.FRIEND_CHANGE";
    public static final String ACTION_SENT_INVITE_FAIL = "com.cngphone.AnFetion.FetionListFriends.SENT_INVITE_FAIL";
    public static final String ACTION_RECONNECT = "com.cngphone.AnFetion.FetionService.RECONNECT_TRY";
    public static final String ACTION_RECONNECT_FAIL = "com.cngphone.AnFetion.FetionService.RECONNECT_TRY_FAIL";
    public static final String ACTION_RECONNECT_SUCCESS = "com.cngphone.AnFetion.FetionService.RECONNECT_SUCCESS";
    public static final String ACTION_RECONNECT_CLEAR = "com.cngphone.AnFetion.FetionService.RECONNECT_CLEAR";
    
    public static final int MENU_DELETE = 10000;
    public static final int MENU_MODIFY = MENU_DELETE + 1;
    public static final String[] FRIEND_MENU = { "删除", "修改信息" };
    public static final int[] FRIEND_MENU_IDS = { MENU_DELETE, MENU_MODIFY };
    public static final int MENU_ADD = MENU_MODIFY + 1;
    public static final int MENU_HELP = MENU_ADD + 1;
    public static final int MENU_QUIT = MENU_HELP + 1;
    public static final int MENU_SETUP = MENU_QUIT + 1;
    public static final int MENU_FINISH = MENU_SETUP + 1;
    public static final String[] LIST_MENU = { "添加", "注销", "退出", "设置", "帮助" };
    public static final int[] LIST_MENU_IDS = { MENU_ADD, MENU_QUIT, MENU_FINISH, MENU_SETUP, MENU_HELP };

    public static final int STATUS_OFFLINE = 0;
    public static final int STATUS_AWAY = 100;
    public static final int STATUS_ONLINE = 400;
    public static final int STATUS_BUSY = 600;
    public static final int STATUS_HIDDEN = -1;
    public static final int STATUS_QUIT = -100;
    
    public static final int TYPE_NOT_FRIEND = 0;
    public static final int TYPE_FRIEND = 1;
    public static final int TYPE_REFUSE = 2;

    public static final CharSequence[] STATUS_MENU = { "在线", "离开", "忙碌", "隐身" };
    public static final int[] STATUS_MENU_IDS = { STATUS_ONLINE, STATUS_AWAY, STATUS_BUSY, STATUS_HIDDEN };

    public static String loginName, loginPass;
    public static int loginStatus;
    public static boolean remember = false;
    public static String[] userlist, passlist, statuslist;
    public static String ssic;

    public static final String getStatusDesc(int status) {
        int size = Settings.STATUS_MENU.length;
        if (status == 0) return "[脱机]";
        for (int i = 0; i < size; i++) {
            if (Settings.STATUS_MENU_IDS[i] == status) {
                return "[" + Settings.STATUS_MENU[i] + "]";
            }
        }
        // System.out.println(status);
        return "[未知]";
    }

    public static int getStatusIndex(String s) {
        int status = StrUtil.parseInt(s, Integer.MIN_VALUE);
        if (status != Integer.MIN_VALUE) {
            int size = Settings.STATUS_MENU_IDS.length;
            for (int i = 0; i < size; i++) {
                if (status == Settings.STATUS_MENU_IDS[i]) {
                    return i;
                }
            }
        }
        return 0;
    }

    public static void loadSetting(Context ctx) {
        SharedPreferences settings = ctx.getApplicationContext().getSharedPreferences(PREFS_USER_RECORD,
                Context.MODE_PRIVATE);
        String s = settings.getString("userList", "");
        userlist = s.split("\t");
        s = settings.getString("passList", "");
        passlist = s.split("\t");
        s = settings.getString("statusList", "");
        statuslist = s.split("\t");
    }

    public static int getUserIndex(String s) {
        if (s.equals("")) return -1;
        for (int i = 0; i < Settings.userlist.length; i++) {
            if (s.equals(Settings.userlist[i])) {
                return i;
            }
        }
        return -1;
    }

    public static void updateSetting(Context ctx) {
        // System.out.println("savesetting");
        if (!remember) return;
        int index = -1;
        int size = Settings.userlist.length + 1;
        if (size > 0) {
            index = getUserIndex(loginName);
            if (index >= 0) {
                size--;
            }
        }
        String[] u1 = new String[size], u2 = new String[size], u3 = new String[size];
        u1[0] = loginName;
        u2[0] = loginPass;
        u3[0] = Integer.toString(loginStatus);
        int l = 1;
        size = Settings.userlist.length;
        for (int i = 0; i < size; i++) {
            if (index != i) {
                u1[l] = Settings.userlist[i];
                u2[l] = Settings.passlist[i];
                u3[l] = Settings.statuslist[i];
                l++;
            }
        }
        Settings.userlist = u1;
        Settings.passlist = u2;
        Settings.statuslist = u3;
        saveSetting(ctx);
    }

    private static void saveSetting(Context ctx) {
        SharedPreferences userRecord = ctx.getApplicationContext().getSharedPreferences(PREFS_USER_RECORD,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = userRecord.edit();
        StringBuilder s1 = new StringBuilder(), s2 = new StringBuilder(), s3 = new StringBuilder();
        int size = Settings.userlist.length;
        for (int i = 0; i < size; i++) {
            if (i > 0) {
                s1.append("\t");
                s2.append("\t");
                s3.append("\t");
            }
            s1.append(Settings.userlist[i]);
            s2.append(Settings.passlist[i]);
            s3.append(Settings.statuslist[i]);
        }
        editor.putString("userList", s1.toString());
        editor.putString("passList", s2.toString());
        editor.putString("statusList", s3.toString());
        editor.commit();

    }

    public static void removeUser(Context ctx, int index) {
        int size = Settings.userlist.length - 1;
        String[] u1 = new String[size], u2 = new String[size], u3 = new String[size];
        int l = 1;
        size = Settings.userlist.length;
        for (int i = 0; i < size; i++) {
            if (index != i) {
                u1[l] = Settings.userlist[i];
                u2[l] = Settings.passlist[i];
                u3[l] = Settings.statuslist[i];
                l++;
            }
        }
        Settings.userlist = u1;
        Settings.passlist = u2;
        Settings.statuslist = u3;
        saveSetting(ctx);
    }
}
